home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / WANDR401.ZIP / sources / MONSTERS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-31  |  5.4 KB  |  206 lines

  1. /*             Monsters.c                     */
  2.  
  3. /*       Functions for animating the monsters */
  4.  
  5.  
  6. #include "wand_head.h"
  7. #include "samples.h"
  8.  
  9. typedef struct { int d[2]; } direction;
  10.  
  11. #ifdef    LINT_ARGS    /* M001 */
  12. direction new_direction(int, int, int, int);
  13. #else
  14. direction new_direction();
  15. #endif
  16.  
  17. extern int debug_disp;
  18. extern char lscreen[NOOFROWS][ROWLEN+1];
  19. extern int audio_flag;
  20.  
  21. /* Add a spirit to the chain */
  22. /* Maintain a doubly linked list to make reuse possible.
  23.    tail_of_list is *NOT* the last monster allocated, but
  24.    the last monster alloted to a screen.  start_of_list
  25.    is a dummy entry to ease processing. last_of_list
  26.    is the last entry allocated. */
  27.  
  28. extern struct mon_rec *last_of_list, *tail_of_list;
  29. extern struct mon_rec start_of_list;
  30.  
  31. struct mon_rec *make_monster(x, y)
  32. int x, y;
  33. {
  34.     char *malloc();
  35. #define MALLOC (struct mon_rec *)malloc(sizeof(struct mon_rec))
  36.     struct mon_rec *monster;
  37.     if (tail_of_list->next == NULL) {
  38.     if ((last_of_list = MALLOC) == NULL)
  39.         return NULL;
  40.     tail_of_list->next = last_of_list;
  41.     last_of_list->prev = tail_of_list;
  42.     last_of_list->next = NULL;
  43.     }
  44.     monster = tail_of_list = tail_of_list->next;
  45.     monster->x = x;
  46.     monster->y = y;
  47.     monster->mx = 1;    /* always start moving RIGHT. (fix later)  */
  48.     monster->my = 0;
  49.     monster->under = ' ';
  50.     return monster;
  51. }
  52.  
  53.  
  54.  
  55.  
  56. /* 'follow lefthand wall' algorithm for baby monsters */
  57.  
  58. direction new_direction(x, y, bx, by)
  59. int x, y, bx, by;
  60. {
  61.     direction out;
  62.  
  63.     if (viable((x+by),(y-bx))) {
  64.     out.d[0] = by;
  65.     out.d[1] = -bx;
  66.     return out;
  67.     }
  68.     if (viable((x+bx),(y+by))) {
  69.     out.d[0] = bx;
  70.     out.d[1] = by;
  71.     return out;
  72.     }
  73.     if (viable((x-by),(y+bx))) {
  74.     out.d[0] = -by;
  75.     out.d[1] = bx;
  76.     return out;
  77.     }
  78.     if (viable((x-bx),(y-by))) {
  79.     out.d[0] = -bx;
  80.     out.d[1] = -by;
  81.     return out;
  82.     }
  83.     out.d[0] = -bx;
  84.     out.d[1] = -by;
  85.     return out;
  86. }
  87.  
  88.  
  89.  
  90. int move_monsters(mxp, myp, score, howdead, sx, sy, nf, 
  91.     x, y, diamonds)
  92. int *mxp, *myp, sx, sy, nf, x, y, diamonds;
  93. long *score;
  94. char *howdead;
  95. {
  96.     int xdirection, ydirection, hd, vd;
  97.     int deadyet = 0;
  98.     int bx, by, nbx, nby, tmpx,tmpy;
  99.     direction new_disp;
  100.     struct mon_rec *monster,*current;
  101.     char buffer[25];
  102.  
  103. /* big monster first */
  104.     if (*mxp == -2) {        /* has the monster been killed ? */
  105.     *score+=100;
  106.     *mxp = *myp = -1;
  107.         notify_score(*score);
  108.     }            /* if monster still alive */
  109.     if (*mxp != -1) {    /* then move that monster ! */
  110.     lscreen[*myp][*mxp] = ' ';
  111.     if (*mxp > x)
  112.         xdirection = -1;
  113.     else
  114.         xdirection = 1;
  115.  
  116.         draw_object(*myp,*mxp,' ');
  117.         refresh_screen();
  118.  
  119.     if ((hd = (*mxp-x))<0)
  120.         hd = -hd;
  121.     if ((vd = (*myp-y))<0)
  122.         vd = -vd;
  123.     if ((hd > vd) && ((*mxp+xdirection) < ROWLEN) &&
  124.         ((lscreen[*myp][*mxp+xdirection] == ' ') ||
  125.         (lscreen[*myp][*mxp+xdirection] == '@')))
  126.         *mxp += xdirection;
  127.     else {
  128.         if (*myp > y)
  129.         ydirection = -1;
  130.         else
  131.         ydirection = 1;
  132.         if (((*myp+ydirection) < NOOFROWS) &&
  133.         ((lscreen[*myp+ydirection][*mxp] == ' ') ||
  134.         (lscreen[*myp+ydirection][*mxp] == '@')))
  135.         *myp += ydirection;
  136.         else if (((*mxp+xdirection) < ROWLEN) &&
  137.         (lscreen[*myp][*mxp+xdirection] == ' ') ||
  138.         (lscreen[*myp][*mxp+xdirection] == '@'))
  139.         *mxp += xdirection;
  140.     }
  141.  
  142.     draw_object(*myp,*mxp,'M');
  143.         refresh_screen();
  144.  
  145.     if (lscreen[*myp][*mxp] == '@') {    /* ha! gottim! */
  146.         strcpy(howdead,"a hungry monster");
  147.         deadyet = 1;
  148.             if(audio_flag) play_audio_sample(KILLED_BY_M_SND);
  149.     }
  150.     lscreen[*myp][*mxp] = 'M';
  151.     }
  152.  
  153.     current = &start_of_list;        /* baby monsters now */
  154.     while ((current != tail_of_list) && (!deadyet)) {
  155.     /* deal with those little monsters */
  156.     monster = current->next;
  157.     new_disp = new_direction(monster->x, monster->y, monster->mx, monster->my);
  158.     if (monster->under != 'S') {    /* if on top of another baby */
  159.         lscreen[monster->y][monster->x] = monster->under;
  160.  
  161.     draw_object(monster->y,monster->x,monster->under);
  162.         refresh_screen();
  163.  
  164.     if (monster->under == ' ')
  165.         deadyet += check(&*mxp,&*myp,monster->x,monster->y
  166.                  ,new_disp.d[0],new_disp.d[1],sx,sy,howdead);
  167.     } else
  168.         monster->under = ' ';
  169.     monster->mx = new_disp.d[0];
  170.     monster->my = new_disp.d[1];
  171.     monster->x += monster->mx;
  172.     monster->y += monster->my;
  173.     monster->under = lscreen[monster->y][monster->x];
  174.     lscreen[monster->y][monster->x] = 'S';    /* move into new space */
  175.     draw_object(monster->y,monster->x,'S');
  176.         refresh_screen();
  177.     if (monster->under == '@') {    /* monster hit you? */
  178.         strcpy(howdead,"the little monsters");
  179.         deadyet = 1;
  180.             if(audio_flag) play_audio_sample(KILLED_BY_S_SND);
  181.         monster->under = ' ';
  182.     }
  183.     if (monster->under == '+') {    /* monster hit cage? */
  184.         *score += 20;
  185.             notify_score(*score);
  186.             if(audio_flag) play_audio_sample(LOCK_S_SND);
  187.         /* remove from chain, and insert at the end (at last_of_list) */
  188.         if (monster == tail_of_list)
  189.         tail_of_list = tail_of_list->prev;
  190.         else {
  191.         current->next = monster-> next;
  192.         current->next->prev = current;
  193.         monster->next = NULL;
  194.         monster->prev = last_of_list;
  195.         last_of_list->next = monster;
  196.         last_of_list = monster;
  197.         }
  198.         lscreen[monster->y][monster->x] = '*';
  199.         draw_object(monster->y,monster->x,'*');
  200.             refresh_screen();
  201.     } else
  202.         current = monster;
  203.     }
  204.     return deadyet;
  205. }
  206.